
<template>
  <el-card style="max-width: 480px">
    <template #header>
      <div class="card-header">
        <span>Smartmeeting智能会议室</span>
      </div>
    </template>
    <el-form :model="form" label-width="auto" :rules="loginRules" ref="loginFormRef"  style="width: 300px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="form.username"  placeholder="请输入用户名" />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="form.password"  type="password" show-password placeholder="请输入密码" />
    
    </el-form-item> 
    <!-- <el-form-item label="上传文件">
      <input type="file" ref="file" @change="upload" />
    </el-form-item> -->
    <el-form-item>
      <el-button type="primary" @click="loginAction">登录</el-button>
      <el-button @click="toRegister">注册</el-button>
    </el-form-item>
  </el-form>
  </el-card>
</template>
<script>
import Login from '../api/login'

export default{
    data(){
        return{
            form:{
                username:'',
                password:''
            },value1:'',
            loginRules: {
                username: [
                  { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                  { required: true, message: '请输入密码', trigger: 'blur' },
                  { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
                ]
              },
              files:[]
        }
    },
    methods:{
      toRegister(){
        this.$router.push({name:"register"})
      },
      // upload(){
      //     let formdata=new FormData();
      //     const file=this.$refs.file.files[0];
      //     // let suffix = file.name.split('.')[1]
      //     // if(this.suffixs.upfilesuffix.indexOf(suffix)>-1){}else{
      //     //   this.$message.info('不允许上传该类文件')
      //     //   return
      //     // }
      //     formdata.set("file",file)
      //     formdata.set("filename",file.name)
          
      //     Login.uploadFile(formdata)
      //       .then(res=>{
      //         console.log(res)
      //       })
      //   },
        loginAction(){
          
          this.$refs.loginFormRef.validate((valid)=>{
            if(valid){
              const result = Login.login(this.form)
              result.then(r=>{
                console.log(r)
                if(r.code==1){
                const user = r.data.data
                localStorage.setItem('userId',user.userId)
                localStorage.setItem("username",user.userName)
                localStorage.setItem("realname",user.realName)
                localStorage.setItem("token",r.data.token)
                if(user.userType==1){
                  this.$router.push({name:"admin"})
                }else{
                    this.$router.push({name:'mybooking'})
                }
              }
              })
              
            }
          })
        }
    }
}
</script>
<style scoped>
button{
    width:100px;
    margin: 0 auto;
}
</style>
